package com.lipengcheng.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lipengcheng.feign.clients.ItemFeignClient;
import com.lipengcheng.feign.clients.UserFeignClient;
import com.lipengcheng.feign.common.dto.Address;
import com.lipengcheng.feign.common.dto.Item;
import com.lipengcheng.order.mapper.OrderMapper;
import com.lipengcheng.order.pojo.Order;
import com.lipengcheng.order.pojo.OrderDetail;
import com.lipengcheng.order.pojo.OrderLogistics;
import com.lipengcheng.order.pojo.PaymentDetail;
import com.lipengcheng.order.service.IOrderService;
import com.lipengcheng.order.service.OrderDetailService;
import com.lipengcheng.order.service.OrderLogisticsService;
import com.lipengcheng.order.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ItemFeignClient client;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private OrderLogisticsService orderLogisticsService;

    @Transactional
    @Override
    public Long submitOrder(PaymentDetail paymentDetail) {
        //      //调用feign,根据itemid查询商品信息
      Item item = client.getById(paymentDetail.getItemId());

      //获取商品价格
      Long price = item.getPrice();
      //获取商品数量
      Integer num = paymentDetail.getNum();
      //计算商品总价
      Long totalFee=price * num;
      //封装Order对象,设置对应数据
      Order order=Order.builder()
              .totalFee(totalFee)
              .paymentType(paymentDetail.getPaymentType())
              .userId(UserHolder.getUserId())
              .status(1)
              .createTime(new Date())
              .build();
      //将Order写入数据库中
        save(order);
      //创建OrderDetail对象,设置对应信息存入数据库
      OrderDetail orderDetail= OrderDetail.builder()
              .orderId(order.getId())
              .itemId(paymentDetail.getItemId())
              .num(paymentDetail.getNum())
              .price(price)
              .spec(item.getSpec())
              .image(item.getImage())
              .createTime(new Date())
              .name(item.getName())
              .build();
      orderDetailService.save(orderDetail);
      //调用feign,查询地址信息
      Address address = userFeignClient.findAddressById(paymentDetail.getAddressId());
      //创建OrderLogistics对象,设置信息写入到数据库
      OrderLogistics logistics=OrderLogistics.builder()
              .orderId(order.getId())
              .contact(address.getContact())
              .mobile(address.getMobile())
              .province(address.getProvince())
              .city(address.getCity())
              .town(address.getTown())
              .street(address.getStreet())
              .createTime(new Date())
              .build();
      orderLogisticsService.save(logistics);
        //用feign远程调用item-service的减库存接口
        try {
            client.reduceStock(paymentDetail.getItemId(),paymentDetail.getNum());
        }catch (Exception e){
            throw new RuntimeException("库存不足");
        }
        return order.getId();

    }
}
